var util = require('../../utils/utils.js');
const app = getApp()
const ydb = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*上个页面传入的内容 */
    //存放会议室信息结构体
    appointmentRoom: null,
    //选择的三天内的第几天
    checkFlag: null,
    //选择第几天的具体日期
    checkMyDate: null,
    //选择开会的时段
    checkDay: null,

    //需要收集的内容
    //申请原因
    needReason: 0,
    //会议主题
    meetingPoint: null,
    //申请描述
    needDes: null,

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //给了预约的会议室、选择的天数和时段
    console.log(options)
    //查找出会议室的信息
    wx.cloud.database().collection("meetingRoom").doc(options.roomID).get({
      success: res => {
        let data = res.data;
        console.log(data)
        that.setData({
          appointmentRoom: data,
          checkFlag: options.checkFlag,
          checkDay: options.checkDay,
          checkMyDate: data.timeList[data.timeList.length - (3 - parseInt(options.checkFlag))].specDate,
          //state: data.timeList[0].openTime.{08:00-09:40}.state
        })
      }
    })

  },
  //选择会议类型
  checkboxChange(e) {
    var that = this
    console.log(e)
    that.setData({
      needReason: parseInt(e.detail.value)
    })
  },
  //会议主题
  myInputT(e) {
    console.log(e)
    var myData = e.detail.value
    this.setData({
      meetingPoint: myData
    })
  },
  //申请描述
  myInputD(e) {
    console.log(e)
    var myData = e.detail.value
    this.setData({
      needDes: myData
    })
  },
  //提交
  async mySubmit() {
    //检测会议主题和申请描述是否为空
    var that = this
    var v = that.data
    console.log(app.globalData.userInfo)

    if (v.meetingPoint == "") {
      wx.showToast({
        title: '未添加会议主题',
        icon: "none"
      })
    } else if (v.needDes == "") {
      wx.showToast({
        title: '未添加申请描述',
        icon: "none"
      })
    } else {
      //确认发布
      let showModalRes = await wx.showModal({
        title: '信息提示',
        content: '建议数据上传过程中如果您强制退出，可能预约建议失败哦～',
        confirmText: "确认",
        showCancel: true,
      })
      if (showModalRes.confirm) {
        console.log("点击了确认");
        var createTime = util.formatTime(new Date()).replace(/-/g, "/").replace(" ", "-");
        wx.showLoading({
          title: '信息上传中',
        })
        console.log(app.globalData.userInfo)
        //需要用到的变量
        //id openid meeetingInfold appointmentTime  checkFlag checkMyDate checkDay needReason meetingPoint needDes isDone isDoneReason
        //调用一个预约云函数完成事务的原子性操作
        let addAppointRes = await wx.cloud.callFunction({
          name: 'addAppointment',
          data: {
            openid: app.globalData.userInfo._openid,
            name: app.globalData.userInfo.nickName,
            img: app.globalData.userInfo.userImg,
            meeetingInfoId: v.appointmentRoom._id,
            meeetingName: v.appointmentRoom.name,
            meetingPublishAdm:v.appointmentRoom._openid,
            appointmentTime: createTime,
            orderTime: Date.now(),
            checkFlag: v.checkFlag,
            checkMyDate: v.checkMyDate,
            checkDay: v.checkDay,
            needReason: v.needReason,
            meetingPoint: v.meetingPoint,
            needDes: v.needDes,
            isDone: 1,
            isDoneReason: "",
            roomStatus: 2,
          },
        })
        console.log(addAppointRes)
        if (addAppointRes.errMsg == "cloud.callFunction:ok") {
          if (addAppointRes.result == "true") {
            wx.hideLoading({})
            let successRes1 = await wx.showModal({
              title: '信息提示',
              content: '预约已申请，可前往我的->我的预约查看！',
              confirmText: "确认",
              showCancel: false,
            })
            if (successRes1.confirm) {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          }
        } else {
          //跳转到添加失败说明页
          wx.navigateTo({
            url: '/pages/someoneAppointment/someoneAppointment',
          })
        }
      }
    }
  },
})